import {createRouter, createWebHistory, RouteRecordRaw} from "vue-router";
// @ts-ignore
import cookie from "js-cookie";
import {getUserInfo} from "@/api/home";
import {message} from "ant-design-vue";

const routes: Array<RouteRecordRaw> = [
    {
        path: "/answer",
        name: 'answer',
        component: () => import('@/views/home/answer/index.vue'),
        meta: {isAuth: false, title: '回答'},
        redirect: "/answer/recommend",
        children: [
            {
                path: "recommend",
                name: 'recommend',
                component: () => import('@/views/home/answer/recommend/index.vue'),
                meta: {isAuth: false, title: '推荐'},
            },
            {
                path: "attention",
                name: 'attention',
                component: () => import('@/views/home/answer/attention/index.vue'),
                meta: {isAuth: true, title: '关注'},
            },
            {
                path: "hot",
                name: 'hot',
                component: () => import('@/views/home/answer/hot/index.vue'),
                meta: {isAuth: false, title: '热榜'},
            }
        ]
    },
    {
        path: "/",
        name: 'article',
        component: () => import('@/views/home/article/index.vue'),
        meta: {isAuth: false, title: '文章'},
    },
    {
        path: "/articleDetails",
        name: 'articleDetails',
        component: () => import('@/views/home/article/articleDetails/index.vue'),
        meta: {isAuth: false, title: '文章详情'},
    },
    {
        path: "/writeArticle",
        name: 'writeArticle',
        component: () => import('@/views/home/article/writeArticle/index.vue'),
        meta: {isAuth: true, title: '写文章'},
    },
    {
        path: "/activity",
        name: 'activity',
        component: () => import('@/views/home/activity/index.vue'),
        meta: {isAuth: false, title: '活动'},
    },
    {
        path: "/activityDetails/:id",
        name: 'activityDetails',
        component: () => import('@/views/home/activity/activityDetails/index.vue'),
        meta: {isAuth: false, title: '活动详情'},
    },
    {
        path: "/makeFriend",
        name: 'makeFriend',
        component: () => import('@/views/home/makeFriend/index.vue'),
        meta: {isAuth: false, title: '交友'},
    },
    {
        path: "/tease",
        name: 'tease',
        component: () => import('@/views/home/tease/index.vue'),
        meta: {isAuth: false, title: '吐槽'},
    },
    {
        path: "/inform",
        name: 'inform',
        component: () => import('@/views/home/inform/index.vue'),
        meta: {isAuth: false, title: '通知'},
        redirect: "/inform/comment",
        children: [
            {
                path: "comment",
                name: 'comment',
                component: () => import('@/views/home/inform/comment/index.vue'),
                meta: {isAuth: true, title: '评论'},
            },
            {
                path: "invite",
                name: 'invite',
                component: () => import('@/views/home/inform/invite/index.vue'),
                meta: {isAuth: true, title: '邀请'},
            },
            {
                path: "fans",
                name: 'fans',
                component: () => import('@/views/home/inform/fans/index.vue'),
                meta: {isAuth: true, title: '新增粉丝'},
            },
            {
                path: "likeAndCollect",
                name: 'likeAndCollect',
                component: () => import('@/views/home/inform/likeAndCollect/index.vue'),
                meta: {isAuth: true, title: '赞和收藏'},
            },
            {
                path: "friendApply",
                name: 'friendApply(inform)',
                component: () => import('@/views/home/inform/friendApply/index.vue'),
                meta: {isAuth: true, title: '好友申请'},
            },
            {
                path: "systemInform",
                name: 'systemInform',
                component: () => import('@/views/home/inform/systemInform/index.vue'),
                meta: {isAuth: true, title: '系统通知'},
            },
        ]
    },
    {
        path: "/problemDetails",
        name: 'problemDetails',
        component: () => import('@/views/home/answer/problemDetails/index.vue'),
        meta: {isAuth: false, title: '问题详情'},
    },
    {
        path: "/my",
        name: 'my',
        component: () => import('@/views/personalCenter/index.vue'),
        meta: {isAuth: true, title: '个人中心'},
        redirect: "/my/userinfo",
        children: [
            {
                path: "userinfo",
                name: 'userinfo',
                component: () => import('@/views/personalCenter/userinfo/index.vue'),
                meta: {isAuth: true, title: '用户信息'},
            },
            {
                path: "myFriends",
                name: 'myFriends',
                component: () => import('@/views/personalCenter/myFriends/index.vue'),
                meta: {isAuth: true, title: '用户信息'},
                redirect: "/my/myFriends/friendList",
                children: [
                    {
                        path: "friendList",
                        name: 'friendList',
                        component: () => import('@/views/personalCenter/myFriends/friendLIst/index.vue'),
                        meta: {isAuth: true, title: '好友列表'},
                    },
                    {
                        path: "friendApply",
                        name: 'friendApply',
                        component: () => import('@/views/personalCenter/myFriends/friendApply/index.vue'),
                        meta: {isAuth: true, title: '好友申请'},
                    },
                ]
            },
            {
                path: "myCollection",
                name: 'myCollection',
                component: () => import('@/views/personalCenter/myCollection/index.vue'),
                meta: {isAuth: true, title: '我的收藏'},
            },
            {
                path: "myAsk",
                name: 'myAsk',
                component: () => import('@/views/personalCenter/myAsk/index.vue'),
                meta: {isAuth: true, title: '我的提问'},
            },
            {
                path: "myArticle",
                name: 'myArticle',
                component: () => import('@/views/personalCenter/myArticle/index.vue'),
                meta: {isAuth: true, title: '我的文章'},
            },
            {
                path: "browsingHistory",
                name: 'browsingHistory',
                component: () => import('@/views/personalCenter/browsingHistory/index.vue'),
                meta: {isAuth: true, title: '浏览历史'},
            },
        ]
    },
    {
        path: "/login",
        name: 'login',
        component: () => import('@/views/login/login.vue'),
        meta: {isAuth: false, title: '登录'},
    },
    {
        path: "/register",
        name: 'register',
        component: () => import('@/views/register/register.vue'),
        meta: {isAuth: false, title: '注册'},
    },
    // {
    //     path: "/chatGPT",
    //     name: 'chatGPT',
    //     component: () => import('@/views/chatGPT/index.vue'),
    //     meta: {isAuth: false, title: 'chatGPT'},
    // }
]

const router = createRouter({
    history: createWebHistory(),
    routes: routes,
})

//前置路由守卫
router.beforeEach((to, from, next) => {
    //是否需要鉴权
    if (to.meta.isAuth) {
        if (cookie.get("userinfo") != null) {
            getUserInfo().then((res) => {
                if (res.data.code === 400) {
                    alert("登录过期，请重新登录!")
                    cookie.remove("token")
                    cookie.remove("userinfo")
                    router.push("/login").then(r => {
                        return r
                    })
                } else {
                    //将用户信息存到cookie中
                    cookie.set("userinfo", JSON.stringify(res.data.data.user))
                    next();
                }
            })
        } else {
            message.warn('未登录，无权限查看!').then(r => {
                return r
            })
        }
    } else {
        next();
    }
})

//后置路由守卫
router.afterEach((to) => {
    document.title = to.meta.title?.toString() || ''
})

export default router